package org.hb0712.sya.tools.database.controller;

import org.hb0712.sya.tools.database.service.TableService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class TableController {
    private Logger logger = LoggerFactory.getLogger(TableController.class);
    @Autowired
    public TableService tableService;
    
    /*
     * 表结构
     */
    @RequestMapping("/table/structure")
    public String structure (Model model, String tablename, String action) {
        if ("reload".equals(action)) {
            tableService.reload();
        }
        
        model.addAttribute("tablename", tablename);
        
        var list = tableService.structure(tablename);
        model.addAttribute("list", list);
        
        var tablenames = tableService.getTableNames();
        model.addAttribute("tablenames", tablenames);
        
        return "structure";
    }

    /*
     * 表数据
     */
    @RequestMapping("/table/data")
    public String data(Model model, String tablename, String TABLE_NAME) {
        model.addAttribute("tablename", tablename);
        model.addAttribute("TABLE_NAME", TABLE_NAME);
        
        var titles = tableService.getTitle(tablename);
        model.addAttribute("titles", titles);
        
        var nameen = tableService.getNameEN(tablename);
        model.addAttribute("nameen", nameen);
        
        var list = tableService.data(tablename, TABLE_NAME);
        model.addAttribute("list", list);
        
        var tablenames = tableService.getTableNames6();
        model.addAttribute("tablenames", tablenames);
        
        return "data";
    }
}
